var utils = require("../BleMash-js/util.js")
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var Remote = require("../../utils/remote.js")
var app = getApp()
Page({
  data:{
    warm:0,
    cold:0,
    colorData: {
      //基础色相，即左侧色盘右上顶点的颜色，由右侧的色相条控制
      hueData: {
        colorStopRed: 255,
        colorStopGreen: 0,
        colorStopBlue: 0,
      },
      //选择点的信息（左侧色盘上的小圆点，即你选择的颜色）
      pickerData: {
        x: 0, //选择点x轴偏移量
        y: 480, //选择点y轴偏移量
        red: 0,
        green: 0,
        blue: 0,
        hex: '#000000'
      },
      //色相控制条的位置
      barY: 0
    },
    rpxRatio: 1 //此值为你的屏幕CSS像素宽度/750，单位rpx实际像素
  },

  onLoad (e) {
    var that = this;
    this.setData({
      rpxRatio: app.globalData.phonewidth / 750
    })
  },
  onChangeColor(e) {
    this.setData({
      colorData: e.detail.colorData
    });
    this.groupControl();
  },

  warmchange (e){
    this.setData({warm:e.detail.value});
    this.groupControl();
  },
  coldchange(e) {
    this.setData({cold: e.detail.value});
    this.groupControl();
  },
  groupControl(){
    var activemesh = app.globalData.activemesh;
    var warm = utils.intToHex(this.data.warm);
    var cold = utils.intToHex(this.data.cold);
    var red = utils.intToHex(this.data.colorData.pickerData.red);
    var green = utils.intToHex(this.data.colorData.pickerData.green);
    var blue = utils.intToHex(this.data.colorData.pickerData.blue);
    activemesh.groupControl(app.globalData.activegroup.groupaddress, cold, warm, red, green, blue, (result)=>{
      Ajax.AsyncPost("/group/group_control/", {
        groupid: app.globalData.activegroup.id,
        cold: this.data.cold,
        warm: this.data.warm,
        red: this.data.colorData.pickerData.red,
        green: this.data.colorData.pickerData.green,
        blue: this.data.colorData.pickerData.blue,
      },(res)=>{
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        var lights = prevPage.data.lightsin
        var i;
        for (i = 0; i < lights.length; i++) {
          lights[i].warm = this.data.warm;
          lights[i].cold = this.data.cold;
          lights[i].red = this.data.colorData.pickerData.red;
          lights[i].green = this.data.colorData.pickerData.green;
          lights[i].blue = this.data.colorData.pickerData.blue;
        }
        prevPage.setData({
          lightsin: lights
        })
      });
    });
  },
  groupOff: function () {
    var mesh = app.globalData.activemesh;
    mesh.groupCloseLight(app.globalData.activegroup.groupaddress);
  },
  remote_control: function () {
    var red = utils.intToHex(this.data.colorData.pickerData.red);
    var green = utils.intToHex(this.data.colorData.pickerData.green);
    var blue = utils.intToHex(this.data.colorData.pickerData.blue);
    var data = {
      addr: app.globalData.activegroup.groupaddress,
      rgb: red + green + blue
    }
    console.log("rgb", data);
    Remote.control(data);
  },
  remote_off: function () {
    var data = {
      addr: app.globalData.activegroup.groupaddress,
      rgb: '000000'
    }
    console.log("rgb", data);
    Remote.control(data);
  }
})
